<?php
require_once 'dal/DaoAbstract.php';
require_once 'model/Person.php';

class PersonDao extends DaoAbstract {
	
	const SELECT_ALL = 
		"SELECT * FROM fullperson";
	
	const SELECT_BY_ID = 
		"SELECT * FROM fullperson WHERE personid = :id";
	
	const SELECT_BY_NAME =
		"SELECT * FROM fullperson WHERE username = :username";
	
	const SELECT_LICENCES_BY_ID = 
		"SELECT classname FROM licence WHERE personid = :id";
	
	private static $DELETE_PERSON = NULL; 
	private static $DELETE_CUSTOMER = NULL;
	private static $DELETE_WEBUSER = NULL;
	private static $DELETE_LICENCES = NULL;
	
	private static $SAVE_PERSON = NULL;
	private static $SAVE_CUSTOMER = NULL;
	private static $SAVE_WEBUSER = NULL;
	private static $SAVE_LICENCE = NULL;
	
	private static $SAVE_PERSON_PARAMS = array("personid","salutation","firstname","lastname",
			"email","born","postcode","street","licencenr","licencesince");
	private static $SAVE_CUSTOMER_PARAMS = array("personid","ccnr","bic","iban","premium");
	private static $SAVE_LICENCE_PARAMS = array("personid","classname");
	private static $SAVE_WEBUSER_PARAMS = array("personid","username","pwhash","salt");
	
	# generate queries from params
	public static function init() {
		self::$SAVE_PERSON = self::applySave("person", self::$SAVE_PERSON_PARAMS);
		self::$SAVE_CUSTOMER = self::applySave("customer", self::$SAVE_CUSTOMER_PARAMS);
		self::$SAVE_LICENCE = self::applySave("licence", self::$SAVE_LICENCE_PARAMS);
		self::$SAVE_WEBUSER = self::applySave("webuser",self::$SAVE_WEBUSER_PARAMS);
		self::$DELETE_CUSTOMER = self::applyDelete("customer", "personid");
		self::$DELETE_WEBUSER = self::applyDelete("webuser", "personid");
		self::$DELETE_LICENCES = self::applyDelete("licence", "personid");
		self::$DELETE_PERSON = self::applyDelete("person", "personid");
	}
	
	public function findById($id) {
		return $this->single(self::SELECT_BY_ID, array("id" => $id));
	}
	
	public function findByUsername($username) {
		return $this->single(self::SELECT_BY_NAME, array("username" => $username));
	}
	
	public function findAll() {
		return $this->multiple(self::SELECT_ALL);	
	}
	
	public function findLicences($id) {
		$result = array();
		$licences = $this->multiple(self::SELECT_LICENCES_BY_ID, array("id" => $id));
		# convert to flat array from column
		foreach ($licences as $curr) {
			$result[] = $curr['classname'];			
		}
		return $result;
	}
	
	public function save($person) {
		$genId = NULL;
		try {
			$conn = $this->conn();
			print "Saving id ".$person['personid'];
			$conn->beginTransaction();
			# save core values first
			$this->exec(self::$SAVE_PERSON, $this->extractParams($person, self::$SAVE_PERSON_PARAMS));
			# retrieve generated id
			$genId = $conn->lastInsertId();
			if ($genId > 0) {	# only use if insert/update actually happened
				$person['personid'] = $genId; 
			} else {
				$genId = $person['personid'];
			}
			# update dependant table
			$this->exec(self::$SAVE_CUSTOMER, $this->extractParams($person, self::$SAVE_CUSTOMER_PARAMS));
			$this->exec(self::$SAVE_WEBUSER, $this->extractParams($person,self::$SAVE_WEBUSER_PARAMS));
			$conn->commit();
		} catch (PDOException $e) {
			echo "!! dao failure on save : ".$e."\nRolling back.\n";
			$conn->rollBack();
			$genId = -1;
		}
		return $genId;
	}
	
	public function addLicence($id,$classes) {
		$result = false;
		try {
			$conn = $this->conn();
			$conn->beginTransaction();
			foreach($classes as $classname) {
				$this->exec(self::$SAVE_LICENCE, array("personid" => $id, "classname" => $classname));
			}
			$conn->commit();
			$result = true;	
		} catch (PDOException $e) {
			echo "!! dao failure on save : ".$e."\nRolling back.\n";
			$conn->rollBack();
			$result = false;
		}
		return $result;
	}
	
	public function delete($id) {
		$result = false;
		try {
			$conn = $this->conn();
			$conn->beginTransaction();
			# delete from least important up
			$this->exec(self::$DELETE_LICENCES, array("personid" => $id));
			$this->exec(self::$DELETE_CUSTOMER, array("personid" => $id));
			$this->exec(self::$DELETE_PERSON, array("personid" => $id));
			$stmt = $this->exec(self::$DELETE_WEBUSER, array("personid" => $id));
			if ($stmt->rowCount() > 0) {
				$result = true;
			}
			$conn->commit();
		} catch (PDOException $e) {
			echo "!! dao failure on delete : ".$e."\nRolling back.\n";
			$conn->rollBack();
			$result = false;
		}
		return $result;
	}
}
PersonDao::init();
?>